package com.nbcio.pm.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.common.satoken.utils.LoginHelper;
import com.nbcio.pm.domain.bo.PmTaskBo;
import com.nbcio.pm.domain.bo.PmTaskDependencyBo;
import com.nbcio.pm.domain.vo.PmTaskVo;
import com.nbcio.pm.domain.vo.PmTaskDependencyVo;
import com.nbcio.pm.service.IPmTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 任务管理Controller
 *
 * @author nbacheng
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/task")
@Tag(name = "任务管理", description = "任务管理相关接口")
public class PmTaskController extends BaseController {

    private final IPmTaskService pmTaskService;

    /**
     * 查询任务列表
     */
    @Operation(summary = "查询任务列表", description = "分页查询任务列表")
    @SaCheckPermission("pm:task:list")
    @GetMapping("/list")
    public TableDataInfo<PmTaskVo> list(PmTaskBo bo, PageQuery pageQuery) {
        return pmTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取任务详细信息
     */
    @Operation(summary = "获取任务详细信息", description = "根据任务ID获取详细信息")
    @SaCheckPermission("pm:task:query")
    @GetMapping("/{taskId}")
    public R<PmTaskVo> getInfo(@Parameter(description = "任务ID") @PathVariable Long taskId) {
        return R.ok(pmTaskService.queryById(taskId));
    }

    /**
     * 新增任务
     */
    @Operation(summary = "新增任务", description = "创建新任务")
    @SaCheckPermission("pm:task:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTaskBo bo) {
        return toAjax(pmTaskService.insertByBo(bo));
    }

    /**
     * 修改任务
     */
    @Operation(summary = "修改任务", description = "更新任务信息")
    @SaCheckPermission("pm:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTaskBo bo) {
        return toAjax(pmTaskService.updateByBo(bo));
    }

    /**
     * 删除任务
     */
    @Operation(summary = "删除任务", description = "删除任务")
    @SaCheckPermission("pm:task:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@Parameter(description = "任务ID数组") @PathVariable Long[] taskIds) {
        return toAjax(pmTaskService.deleteWithValidByIds(Arrays.asList(taskIds), true));
    }

    /**
     * 获取任务树
     */
    @Operation(summary = "获取任务树", description = "获取项目的任务树结构")
    @SaCheckPermission("pm:task:query")
    @GetMapping("/tree/{projectId}")
    public R<List<PmTaskVo>> getTaskTree(@Parameter(description = "项目ID") @PathVariable Long projectId) {
        return R.ok(pmTaskService.getTaskTree(projectId));
    }

    /**
     * 更新任务状态
     */
    @Operation(summary = "更新任务状态", description = "更新任务状态")
    @SaCheckPermission("pm:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{taskId}/status")
    public R<Void> updateStatus(@Parameter(description = "任务ID") @PathVariable Long taskId, 
                                @Parameter(description = "任务状态") @RequestParam String status) {
        return toAjax(pmTaskService.updateTaskStatus(taskId, status));
    }

    /**
     * 分配任务
     */
    @Operation(summary = "分配任务", description = "分配任务给指定用户")
    @SaCheckPermission("pm:task:assign")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{taskId}/assign")
    public R<Void> assignTask(@Parameter(description = "任务ID") @PathVariable Long taskId, 
                              @Parameter(description = "负责人ID") @RequestParam Long assigneeId) {
        return toAjax(pmTaskService.assignTask(taskId, assigneeId));
    }

    /**
     * 获取我的任务
     */
    @Operation(summary = "获取我的任务", description = "获取当前用户的任务列表")
    @GetMapping("/my-tasks")
    public R<List<PmTaskVo>> getMyTasks(@Parameter(description = "任务状态") @RequestParam(required = false) String status) {
        Long userId = LoginHelper.getUserId();
        return R.ok(pmTaskService.getUserTasks(userId, status));
    }

    /**
     * 获取任务依赖关系
     */
    @Operation(summary = "获取任务依赖关系", description = "获取任务的依赖关系列表")
    @SaCheckPermission("pm:task:query")
    @GetMapping("/{taskId}/dependencies")
    public R<List<PmTaskDependencyVo>> getTaskDependencies(@Parameter(description = "任务ID") @PathVariable Long taskId) {
        return R.ok(pmTaskService.getTaskDependencies(taskId));
    }

    /**
     * 添加任务依赖
     */
    @Operation(summary = "添加任务依赖", description = "添加任务依赖关系")
    @SaCheckPermission("pm:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/dependency")
    public R<Void> addTaskDependency(@Validated(AddGroup.class) @RequestBody PmTaskDependencyBo bo) {
        return toAjax(pmTaskService.addTaskDependency(bo));
    }

    /**
     * 删除任务依赖
     */
    @Operation(summary = "删除任务依赖", description = "删除任务依赖关系")
    @SaCheckPermission("pm:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/dependency/{dependencyId}")
    public R<Void> removeTaskDependency(@Parameter(description = "依赖ID") @PathVariable Long dependencyId) {
        return toAjax(pmTaskService.removeTaskDependency(dependencyId));
    }
}
